package com.cxf.windex.base.util;

import java.util.Map;

/**
 * @program: cxf-mf
 * @description: map与对象转换工具类
 * @author: chenxf
 * @create: 2018-05-30 17:18
 **/
public class MapUtil {
  /*
   * Populate the JavaBeans properties of the specified bean, based on
   * the specified name/value pairs.
   */
  public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
    if (map == null)
      return null;

    Object obj = beanClass.newInstance();

    org.apache.commons.beanutils.BeanUtils.populate(obj, map);

    return obj;
  }

  /*
   *  An implementation of Map for JavaBeans which uses introspection to
   * get and put properties in the bean.
   */
  public static Map<?, ?> objectToMap(Object obj)  throws Exception {
    if(obj == null)
      return null;

    return new org.apache.commons.beanutils.BeanMap(obj);
  }
}
